package com.tjian.fafa.user.base.myNetwork;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.tjian.fafa.user.base.ConnectedApp;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.commons.GlobalMsgTypes;

//BroadcastReceiver也就是“广播接收者”的意思，顾名思义，它就是用来接收来自系统和应用中的广播。
//BroadcastReceiver用于接收程序（包括系统程序和一般应用）通过sendBroadcast()方法发出的Broadcast intents。
public class NetStateReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (currentNetworkInfo.isConnected())
        {
            if (NetworkService.getInstance().getIsConnected())
            {
                UserInfo userInfo = ConnectedApp.getInstance().getUserInfo();

                if (userInfo == null)
                {
                    return;
                }

                NetworkService.getInstance().sendUpload(GlobalMsgTypes.msgBackOnline, userInfo.toString());

            }


        }
        else
        {
            NetworkService.getInstance().closeConnection();
        }
    }

}